function SortableTable(tableEl) {

    var unthead = false;
    var sortRow2 = [];

    this.tbody = [tableEl.tBodies[0]];//tableEl.getElementsByTagName('tbody');
    this.thead = [tableEl.tHead];//tableEl.getElementsByTagName('thead');
    this.tfoot = [tableEl.tFoot];//tableEl.getElementsByTagName('tfoot');
    var heads = document.getElementsByClassName("iqarothead");

    for (var i = 0; i < heads.length; i++) {
        try {
            if (heads[i].parentNode.className == "fixedheader xthf-no-print paginaT") {
                sortRow2.push(heads[i]);
            }
        } catch (e) {
            if (heads[i].getParent().className == "fixedheader xthf-no-print paginaT") {
                sortRow2.push(heads[i]);
            }
        }
    }

    this.getInnerText = function (el) {
        if (typeof (el.textContent) != 'undefined') {
            var m = "";
            if (el.childNodes != null) {
                for (var i = 0; i < el.childNodes.length; i++) {
                    if (el.childNodes[i].tagName == 'LABEL') {
                        try {
                            m = el.childNodes[i].getAttribute("v");
                            if (m == null) {
                                m = "";
                            }
                        } catch (e) {
                            m = "";
                        }
                        i = el.childNodes.length - 1;
                        break;
                    }
                }
            }

            if (m == "")
                return el.textContent;
            else
                return m;
        }
        if (typeof (el.innerText) != 'undefined') {
            alert("el.innerText>>>tagName" + el.tagName + "innerHTML" + el.innerHTML + "" + el.innerText);
            return el.innerText;
        }
        if (typeof (el.innerHTML) == 'string') {
            alert("el.innerHTML>>>tagName" + el.tagName + "innerHTML" + el.innerHTML + "" + el.innerHTML.replace(/<[^<>]+>/g, ''));
            return el.innerHTML.replace(/<[^<>]+>/g, '');
        }
    }

    this.getParent = function (el, pTagName) {
        if (el == null)
            return null;
        else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase())
            return el;
        else
            return this.getParent(el.parentNode, pTagName);
    }

    this.sort = function (cell) {
        var column = cell.cellIndex;
        var itm = this.getInnerText(this.tbody[0].rows[1].cells[column]);
        var sortfn = this.sortCaseInsensitive;

        if (itm.match(/\d\d[-]+\d\d[-]+\d\d\d\d/))
            sortfn = this.sortDate; // date format mm-dd-yyyy
        if (itm.replace(/^\s+|\s+$/g, "").match(/^[\d\.]+$/))
            sortfn = this.sortNumeric;

        if (itm.trim() == "")
            sortfn = this.sortNumeric;

        this.sortColumnIndex = column;

        var newRows = new Array();
        for (j = 0; j < this.tbody[0].rows.length; j++) {
            newRows[j] = this.tbody[0].rows[j];
        }

        try {
            newRows.sort(sortfn);
        } catch (e) {
            newRows.sort(this.sortCaseInsensitive);
        }

        if (cell.getAttribute("sortdir") == 'down') {
            newRows.reverse();
            cell.setAttribute('sortdir', 'up');
        } else {
            cell.setAttribute('sortdir', 'down');
        }

        for (i = 0; i < newRows.length; i++) {
            for (var j = 0; j < newRows[i].cells.length; j++) {
                if (newRows[i].cells[j].getAttribute("ti_pagreg") == "2") {
                    newRows[i].cells[j].innerHTML = i + 1;
                    break;
                }
            }
            this.tbody[0].appendChild(newRows[i]);
        }

    }

    this.sortCaseInsensitive = function (a, b) {
        aa = thisObject.getInnerText(a.cells[thisObject.sortColumnIndex]).toLowerCase();
        bb = thisObject.getInnerText(b.cells[thisObject.sortColumnIndex]).toLowerCase();
        if (aa == bb)
            return 0;
        if (aa < bb)
            return -1;
        return 1;
    }

    this.sortDate = function (a, b) {
        aa = thisObject.getInnerText(a.cells[thisObject.sortColumnIndex]);
        bb = thisObject.getInnerText(b.cells[thisObject.sortColumnIndex]);
        date1 = aa.substr(6, 4) + aa.substr(3, 2) + aa.substr(0, 2);
        date2 = bb.substr(6, 4) + bb.substr(3, 2) + bb.substr(0, 2);
        if (date1 == date2)
            return 0;
        if (date1 < date2)
            return -1;
        return 1;
    }

    this.sortNumeric = function (a, b) {
        aa = parseFloat(thisObject.getInnerText(a.cells[thisObject.sortColumnIndex]));
        if (isNaN(aa))
            aa = 0;
        bb = parseFloat(thisObject.getInnerText(b.cells[thisObject.sortColumnIndex]));
        if (isNaN(bb))
            bb = 0;
        return aa - bb;
    }

    // define variables
    var thisObject = this;
    var sortSection = this.thead;

    // constructor actions
    if (!(this.tbody && this.tbody[0].rows && this.tbody[0].rows.length > 0))
        return;

    if (sortSection && sortSection[0].rows && sortSection[0].rows.length > 0) {
        var sortRow = sortSection[0].rows[2];
    } else {
        return;
    }


    for (var i = 0; i < sortRow.cells.length; i++) {
        sortRow.cells[i].sTable = this;
        var x = sortRow.cells[i];
        var y = x.getElementsByTagName("INPUT");
        var cbo = false;
        for (var j = 0; j < y.length; j++) {
            if (y[j].type == "checkbox") {
                cbo = true;
            }
        }
        if (cbo == false) {
            sortRow.cells[i].setAttribute("onclick", "this.sTable.sort(this); return false;");
        }
    }

    for (var p = 0; p < sortRow2.length; p++) {
        if (sortRow2[p].id == tableEl.getElementsByClassName("iqarothead")[0].id) {
            var tds = sortRow2[p].getElementsByTagName("TD");
            for (var f = 0; f < tds.length; f++) {
                try {
                    if (tds[f].getElementsByTagName("INPUT")[0].type != "checkbox") {
                        tds[f].setAttribute("onclick", "new SortableTable(document.getElementById('" + tableEl.id + "')).sort(this);return false;")
                    }
                } catch (e) {
                    tds[f].setAttribute("onclick", "new SortableTable(document.getElementById('" + tableEl.id + "')).sort(this);return false;");
                }
            }
        }
    }

}